package com.yao.mall.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yao.mall.annotation.AuthCheck;
import com.yao.mall.common.ErrorCode;
import com.yao.mall.common.Result;
import com.yao.mall.config.BusinessException;
import com.yao.mall.model.dto.AttrKeyAddRequest;
import com.yao.mall.model.dto.AttrKeySearchRequest;
import com.yao.mall.model.dto.AttrKeyUpdateRequest;
import com.yao.mall.model.entity.AttrKey;
import com.yao.mall.model.entity.Category;
import com.yao.mall.model.vo.AttrKeyVO;
import com.yao.mall.service.AttrKeyService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jiahua.yao
 * @version 1.0
 * @description 属性key的控制层
 * @date 2024/3/16 17:58
 */
@RestController
@RequestMapping("/attrKey")
@Slf4j
@Tag(name = "属性名相关接口")
public class AttrKeyController {
    @Resource
    private AttrKeyService attrKeyService;

    @GetMapping("/get")
    @Operation(summary = "获取属性key")
    public Result<List<AttrKeyVO>> getAttrKey() {
        List<AttrKeyVO> attrKeyList = attrKeyService.getAttrKey();
        return Result.success(attrKeyList);
    }

    @PostMapping("/getByConditions")
    @Operation(summary = "条件获取属性key")
    public Result<Page<AttrKeyVO>> getAttrKeyByConditions(@RequestBody AttrKeySearchRequest attrKeySearchRequest) {
        Page<AttrKeyVO> attrKeyList = attrKeyService.getAttrKeyByConditions(attrKeySearchRequest);
        return Result.success(attrKeyList);
    }

    @PostMapping("/add")
    @AuthCheck(mustRole = "1")
    @Operation(summary = "添加属性名")
    public Result<Boolean> updateAttrKey(@RequestBody AttrKeyAddRequest attrKeyAddRequest) {
        Boolean aBoolean = attrKeyService.addAttrKey(attrKeyAddRequest);
        return Result.success(aBoolean);
    }

    @PutMapping("/update")
    @AuthCheck(mustRole = "1")
    @Operation(summary = "修改属性名")
    public Result<Boolean> updateAttrKey(@RequestBody AttrKeyUpdateRequest attrKeyUpdateRequest) {
        if(attrKeyUpdateRequest == null){
            throw new BusinessException(ErrorCode.NULL_ERROR,"必要参数为空");
        }
        Boolean aBoolean = attrKeyService.updateAttrKey(attrKeyUpdateRequest);
        return Result.success(aBoolean);
    }

    @DeleteMapping ("/{id}")
    @Operation(summary = "删除属性名")
    public Result<Boolean> deleteAttrKey(@PathVariable Long id) {
        Boolean aBoolean = attrKeyService.deleteAttrKey(id);
        return Result.success(aBoolean);
    }

}
